import java.util.*;

public class test {
    // 蓝桥杯 8puzzle(⭐⭐⭐⭐)
    class CustomKey{
        private int[][] grid;
        public CustomKey(int[][] grid){
            this.grid = grid;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            CustomKey customKey = (CustomKey) o;
            return Arrays.equals(grid, customKey.grid);
        }

        @Override
        public int hashCode() {
            return Arrays.hashCode(grid);
        }
        public int[][] getGrid(){
            return grid;
        }
    }
    public class Main {
        public static int[] dx = {1,-1,0,0};
        public static int[] dy = {0,0,1,-1};
        public static int[][] nums;
        public static int[][] end;
        public static int num;
        public static int startx;
        public static int starty;
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            nums = new int[3][3];
            end = new int[3][3];
            for(int i = 0;i < 3;i++) {
                for(int j = 0;j < 3;j++) {
                    end[i][j] = i * 3 + j + 1;
                    end[i][j] = i * 3 + j + 1;
                    nums[i][j] = (int) in.next().charAt(0) - '0';
                    if(nums[i][j] == 'x' - '0') {
                        startx = i;
                        starty = j;
                    }
                }
            }
            end[2][2] = 'x' - '0';
            System.out.println(dfs());
        }
        public static int dfs() {
            // 1.存储网格和上次操作到的index('x'的位置)
            // 2.记录网格是否出现过,剪枝
            HashMap<CustomKey, int[]> map = new HashMap<>();
            map.put(new CustomKey(nums),new int[] {startx,starty});
            Queue<int[][]> queue = new LinkedList<>();
            int len = 0;
            queue.add(nums);
            while(!queue.isEmpty()) {
                int size = queue.size();
                for(int j = 0;j < size;j++) {
                    int[][] arr = queue.poll();
                    if(decide(arr)) {
                        return len;
                    }
                    int[] t = map.get(arr);
                    if(t == null){
                        continue;
                    }
                    for(int i = 0;i < 4;i++) {
                        int x = t[0] + dx[i];
                        int y = t[1] + dy[i];
                        // 1.保证x,y合法
                        if(x >= 0 && x < 3 && y >= 0 && y < 3) {
                            // 2.移动'x'
                            int[][] newArr = deepCopy(arr);
                            int temp = newArr[t[0]][t[1]];
                            newArr[t[0]][t[1]] = newArr[x][y];
                            newArr[x][y] = temp;
                            CustomKey newGrid = new CustomKey(newArr);
                            if (!map.containsKey(newGrid)) {
                                map.put(newGrid, new int[]{x, y});
                                queue.add(newArr);
                            }
                        }
                    }
                }
                len++;
            }
            return 0;
        }
        //判断当前网格是否为目标网格
        public static boolean decide(int[][] arr) {
            for(int i = 0;i < 3;i++) {
                for(int j = 0;j < 3;j++) {
                    if(end[i][j] != arr[i][j]) {
                        return false;
                    }
                }
            }
            return true;
        }
        public static int[][] deepCopy(int[][] original) {
            int[][] copy = new int[original.length][];
            for (int i = 0; i < original.length; i++) {
                copy[i] = Arrays.copyOf(original[i], original[i].length);
            }
            return copy;
        }
}
